var youtubeplayer = {};


$(function() {
	var player = {};
    player.install = function(divId, media, startTime, callback) {
		installYoutube(divId, media, startTime, callback);
	}
	player.seek = function(time) {
		youtubeplayer.ytplayer.seekTo(time, true);
	}
	player.time = function() {
		return youtubeplayer.ytplayer.getCurrentTime();
	}
	
	playerRegistry['Youtube'] = player;

})


function onPlayerStateChange(newState) {
	if (newState == 1) {
		youtubeplayer.callback();
	}
}


function installYoutube(divId, media, startTime, callback) {
	youtubeplayer.id = 'ytPlayer';
	youtubeplayer.media = media;
	youtubeplayer.startTime = startTime;
	youtubeplayer.callback = callback;
	
	// Lets Flash from another domain call JavaScript
  var params = { allowScriptAccess: "always" };
  // The element id of the Flash embed
  var atts = { id: "ytPlayer" };
  // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
  swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
                     "&enablejsapi=1&playerapiid=player1&autoplay=1", 
                     divId, "480", "295", "8", null, null, params, atts);
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
     var ytplayer = document.getElementById("ytPlayer");
	 youtubeplayer.ytplayer = ytplayer;
     ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
	 ytplayer.loadVideoById(youtubeplayer.media.video, youtubeplayer.startTime );
}

